<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@page import="java.sql.*" %>
<%request.setCharacterEncoding("utf-8");%>
<%String StuNo=request.getParameter("stuid");%>
<%String stuName=request.getParameter("stuname");%>
<%String stuBirthday=request.getParameter("stubirthday");%>
<%String stuSex=request.getParameter("stusex");%>
<%String stuPoliticalstatus=request.getParameter("stupoliticalstatus");%>
<%String stuIdentitycard=request.getParameter("stuidentitycard");%>
<%String stuSpeciality=request.getParameter("stuspeciality");%>
<%String stuAddress=request.getParameter("stuaddress");%>
<%String stuPostcode=request.getParameter("stupostcode");%>
<%String stuTelephone=request.getParameter("stutelephone");%>
<%String stuEmail=request.getParameter("stuemail");%>
<html>
<head>
    <title>添加学生信息</title>
    <%! ResultSet rs;%>
    <%
        String dbName = "student";
        String userName = "root";
        String userPasswd = "123456";
        String url = "jdbc:mysql://172.21.32.100/" + dbName + "?user=" + userName + "&password=" + userPasswd+"&useUnicode=true&characterEncoding=utf-8";
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection conn = DriverManager.getConnection(url);
        Statement statement = conn.createStatement();

        String sql_select="select * from student where stu_no="+StuNo;
        rs=statement.executeQuery(sql_select);
        if (rs.next()){
            out.print(" <script type='text/javascript'>alert('当前学号已存在');window.history.back(-1);</script> ");
        }
        else {
            String sql = "INSERT into student(stu_no,stu_name,stu_sex,stu_politicalstatus,stu_birthday," + "stu_identitycard,stu_speciality,stu_address,stu_postcode,stu_telephone,stu_email)\n" +
                    "VALUES ('"+StuNo+"','"+stuName+"','"+stuSex+"','"+stuPoliticalstatus+"','"+stuBirthday+"" + "','"+stuIdentitycard+"','"+stuSpeciality+"','"+stuAddress+"','"+stuPostcode+"','"
                    +stuTelephone+"','"+stuEmail+"')";
            int result= statement.executeUpdate(sql);
            if (result>0)
                out.print(" <script type='text/javascript'>alert('添加成功');</script> ");
            else
                out.print(" <script type='text/javascript'>alert('添加失败');</script> ");
            sql = "select * from student";
            rs = statement.executeQuery(sql);
        }
    %>
</head>
<body>
<table border="1">
    <th colspan="7" style="font-size: x-large; alignment: center">学生信息表</th>
    <tr style="text-align: center">
        <td>学号</td>
        <td>姓名</td>
        <td>性别</td>
        <td>政治身份</td>
        <td>出生日期</td>
        <td>身份证号</td>
        <td>所在系部</td>
    </tr>
    <% while (rs.next()) { %>
    <tr style="text-align: center">
        <td><%= rs.getString(1) %></td>
        <td><%= rs.getString(2) %></td>
        <td><%= rs.getString(3) %></td>
        <td><%= rs.getString("stu_politicalstatus") %></td>
        <td><%= rs.getString("stu_birthday") %></td>
        <td><%= rs.getString("stu_identitycard") %></td>
        <td><%= rs.getString("stu_speciality") %></td>
    </tr>
    <% } %>

</table>
</body>
</html>
